package org.example.stuview;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;

public class ScoreView extends JFrame implements Runnable, ActionListener {
    String stuID,stuName;
    static int score;
    JButton back;
    Socket socket=new Socket();
    DataInputStream in=null;
    DataOutputStream out=null;
    Thread thread=new Thread(this);
    InetAddress address= null;
    JLabel IDText;

    public ScoreView(){}

    public void init(){
        this.setSize(525,300);
        this.setDefaultCloseOperation(3);
        this.setLocationRelativeTo(null);
        this.setAlwaysOnTop(true);
        this.setLayout(null);
        this.setVisible(true);
        this.setTitle("成绩页面");

        //打印学生成绩
        Font font = new Font(null,1,20);
        IDText = new JLabel("恭喜"+stuID+"回主页查询成绩吧");
        IDText.setFont(font);
        IDText.setBounds(135,75,525,35);
        this.getContentPane().add(IDText);
        //返回学生界面 按钮
        back = new JButton("返回主界面");
        back.setBounds(175,175,160,35);
        this.getContentPane().add(back);
    }

    public void scoreListener(){
        back.addActionListener(this);
    }

    @Override
    public void run() {}

    @Override
    public void actionPerformed(ActionEvent e) {

        try {
            address = InetAddress.getByName("127.0.0.1");
            InetSocketAddress socketAddress=new InetSocketAddress
                    (address,4331);
            socket.connect(socketAddress);
            in =new DataInputStream(socket.getInputStream());
            out = new DataOutputStream(socket.getOutputStream());
            thread.start();
        } catch (IOException ex) {
            socket=new Socket();
        }

        if (e.getSource() == back){
            this.setVisible(false);

            StudentView studentView = new StudentView();
            studentView.stuID = this.stuID;
            studentView.stuName = this.stuName;
            studentView.init();
            studentView.studentListener();
        }
    }
    public String getStuID() {
        return stuID;
    }

    public int getScore() {
        return score;
    }
}
